const Module = require('webpack/lib/Module');
const {RawSource} = require('webpack-sources');
const DelegatedSourceDependency = require('./dependencies/DelegatedSourceDependency');
class DelegatedModule extends Module {
    //sourceRequest "dll-reference _dll_utils"
    //resolved={id:"./node_modules/_isarray@2.0.5@isarray/index.js"}}
    //originalModule 原始模块 NormalModule {id:"./node_modules/_isarray@2.0.5@isarray/index.js"}
    constructor(sourceRequest,data,originalRequest){
        super('javascript/dynamic');
        this.sourceRequest=sourceRequest;
        this.request = data.id;
        this.originalRequest=originalRequest;
    }
    libIdent(options){//获取此模块的ID的
        return this.originalRequest.libIdent(options);
    }
    identifier(){
        //delegated "./node_modules/_isarray@2.0.5@isarray/index.js"  from "dll-reference _dll_utils"
        return `delegated ${this.request} from ${this.sourceRequest}`;
    }
    readableIdentifier(){
        //delegated "./node_modules/_isarray@2.0.5@isarray/index.js"  from "dll-reference _dll_utils"
        return `delegated ${this.request} from ${this.sourceRequest}`;
    }
    size(){
        return 42;
    }
    //当你先创建出一个模块,然后会给这个模 块进行编译
    build(options,compilation,resolver,fs,callback){
        this.built = true;
        this.buildMeta = {};
        this.buildInfo = {};
        //不走真正的读文件,转语法树,直接添加依赖
        this.delegatedSourceDependency = new DelegatedSourceDependency(this.sourceRequest);
        //把这个依赖添加当前模块的依赖数组中
        this.addDependency(this.delegatedSourceDependency);
        callback();
    }
    //source会返回一个源代码对象
    source(){
        let str = `module.exports = (__webpack_require__("${this.sourceRequest}"))("${this.request}");`;
        return new RawSource(str);
    }
}

module.exports = DelegatedModule;